package com.cms.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cms.domain.Feedback;
import com.cms.entity.Menu;
import com.cms.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Controller - 菜单
 * 
 * 
 * 
 */
@Controller("adminMenuController")
@RequestMapping("/admin/menu")
public class MenuController extends BaseController{

	@Autowired
	private MenuService menuService;

	/**
	 * 列表
	 */
	@RequestMapping
	public String index(ModelMap modelMap) {
		setListQuery();
		modelMap.addAttribute("menuTree", menuService.findTree());
		return getView("menu/index");
	}
	
	
	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(ModelMap modelMap) {
		modelMap.addAttribute("menuTree", menuService.findTree());
		return getView("menu/add");
	}
	
    
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(Menu menu) {
		if(menu.getIsShow()==null){
			menu.setIsShow(false);
        }
		menu.setValue();
		menu.setCreateDate(LocalDateTime.now());
		menu.setUpdateDate(LocalDateTime.now());
		menuService.save(menu);
		return "redirect:"+(getListQuery("/admin/menu"));
	}

	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap modelMap) {
		Menu menu = menuService.getById(id);
		modelMap.addAttribute("menuTree", menuService.findTree());
		modelMap.addAttribute("menu", menu);
		return getView("menu/edit");
	}

	/**
	 * 更新
	 */
	@RequestMapping("/update")
	public String update(Menu menu) {
		if(menu.getIsShow()==null){
			menu.setIsShow(false);
        }
		menu.setValue();
		menu.setUpdateDate(LocalDateTime.now());
		menuService.updateById(menu);
		return "redirect:"+(getListQuery("/admin/menu"));
	}
	
	/**
	 * 修改是否显示
	 */
	@ResponseBody
	@RequestMapping("/updateIsShow")
	public Feedback updateIsShow(Boolean isShow, Integer id){
		Menu menu = menuService.getById(id);
		menu.setIsShow(isShow);
		menu.setUpdateDate(LocalDateTime.now());
		menuService.updateById(menu);
		return Feedback.success(new HashMap<>());
	}
	
   /**
     * 修改排序
     */
   	@ResponseBody
   	@RequestMapping("/updateSort")
    public Feedback updateSort(String sortArray){
        JSONArray jsonArray = JSONArray.parseArray(sortArray);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Integer id = jsonObject.getInteger("id");
            Integer sort = jsonObject.getInteger("sort");
            Menu menu = menuService.getById(id);
            menu.setSort(sort);
			menuService.updateById(menu);
        }
        return Feedback.success(new HashMap<>());
    }

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer id) {
		Menu menu = menuService.getById(id);
		if (menu == null) {
			return Feedback.error("菜单不存在");
		}
		List<Menu> children = menu.getChildren();
		if (children != null && !children.isEmpty()) {
			return Feedback.error("存在下级菜单，无法删除");
		}
		menuService.removeById(id);
		return Feedback.success(new HashMap<>());
	}

}
